package com.terracloud.server.dao.model.sql;

import com.terracloud.server.common.data.Role;
import com.terracloud.server.common.data.id.RoleId;
import com.terracloud.server.common.data.id.TenantId;
import com.terracloud.server.dao.model.ToData;
import com.terracloud.server.dao.util.mapping.SetJsonConverter;
import jakarta.persistence.*;
import lombok.Data;

import java.util.Set;
import java.util.UUID;

/**
 *
 * @author chenhainan
 * @since 2025/10/17
 */
@Data
@Entity
@Table(name = "role")
public class RoleEntity implements ToData<Role> {

    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private UUID id;

    private UUID tenantId;

    private String name;

    @Convert(converter = SetJsonConverter.class)
    private Set<String> menus;

    @Override
    public Role toData() {
        final var role = new Role();
        role.setRoleId(new  RoleId(id));
        role.setName(name);
        role.setTenantId(TenantId.fromUUID(tenantId));
        role.setMenus(menus);
        return role;
    }

    public RoleEntity() {
    }

    public RoleEntity(Role role) {
        if (role.getRoleId() != null) {
            this.id = role.getRoleId().getId();
        }
        this.name = role.getName();
        this.tenantId = role.getTenantId().getId();
        this.menus = role.getMenus();
    }
}
